import BaseLayout from '../layouts/menulayou.vue';
import http from 'webosutils/lib/http';
import { useAppStore } from '../store/app';
import { createRouter, createWebHashHistory,createWebHistory, NavigationGuardNext, RouteLocationNormalized  } from 'vue-router'
import newsdetailVue from '../views/news/newsdetail.vue';
export const inRoutes =[
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/',

    component: BaseLayout,
    meta: { title: '首页' },
    children: [
      {
        path: '/main',
        component: () => import('../views/home/index.vue'),
        meta: { title: '首页' },
      }
    ]
  }, {
    path: '/error',
    component: BaseLayout,
    meta: { title: '错误页面' },
    children: [
      // {
      //   path: '/error/401',
      //   component: () => import('../../views/error/401.vue'),
      //   meta: { title: '401' },
      // },
      // {
      //   path: '/error/403',
      //   component: () => import('../../views/error/403.vue'),
      //   meta: { title: '403' },
      // },
      {
        path: '/error/404',
        component: () => import('../views/error/404.vue'),
        meta: { title: '404' },
      }
      // {
      //   path: '/error/500',
      //   component: () => import('../../views/error/500.vue'),
      //   meta: { title: '500' },
      // }
    ]
  },
  {
    path: '/news',

    component: BaseLayout,
    meta: { title: '工作空间' },
    children: [
      {
        path: '/news/list',
        component: () => import('../views/news/index.vue'),
        meta: { title: '博文', keywords:'xxx',
        description:'xxx'},
      },
      {
        path: '/news/ppt',
        component: () => import('../views/news/ppt.vue'),
        meta: { title: '免费ppt模板下载', keywords:'ppt免费下载',
        description:'迅雷下载免费ppt下载|精美ppt模板' },
      },{
        path: '/news/down',
        component: () => import('../views/news/down.vue'),
        meta: { title: '详情免费ppt模板下载', keywords:'xxx',
        description:'xxx' },
      },
      
      {
        path: '/news/newsdetail',
        component:newsdetailVue,
        meta: { title: '详情' , keywords:'xxx',
        description:'xxx'},
      },
      
      {
        path: '/news/taobao',
        component: () => import('../views/news/taobao.vue'),
        meta: { title: '淘券券', keywords:'xxx',
        description:'xxx' },
      },

      {
        path: '/news/product',
        component: () => import('../views/news/product.vue'),
        meta: { title: '常用分享' , keywords:'常用应用',
        description:'推广'},
      },
      
    ]
  },{
    path: '/webos',
    component: BaseLayout,
    meta: { title: 'webos' },
      children: [
      {
        path: '/webos/doc',
        component: () => import('../views/webos/doc.vue'),
        meta: { title: '首页', keywords:'xxx',
        description:'xxx' },
        children:[{
        
          path: '/webos/doc/detail/:type',
          component: () => import('../views/webos/detail.vue'),
          meta: { title: '首页' , keywords:'xxx',
          description:'xxx'},
        }]
      }
    ]
  },{
    path: '/vuewebos',
    component: BaseLayout,
    meta: { title: 'vuewebos' },
      children: [
      {
        path: '/vuewebos/doc',
        component: () => import('../views/vuewebos/doc.vue'),
        meta: { title: '首页', keywords:'xxx',
        description:'xxx' },
        children:[{
        
          path: '/vuewebos/doc/detail/:type',
          component: () => import('../views/webos/detail.vue'),
          meta: { title: '首页' , keywords:'xxx',
          description:'xxx'},
        }]
      }
    ]
  },{
    path: '/webosapp',
    component: BaseLayout,
    meta: { title: 'webosapp' },
      children: [
      {
        path: '/webosapp/doc',
        component: () => import('../views/vuewebos/doc.vue'),
        meta: { title: '首页', keywords:'xxx',
        description:'xxx' },
 
        children:[{
        
          path: '/webosapp/doc/detail/:type',
          component: () => import('../views/webos/detail.vue'),
          meta: { title: '首页', keywords:'xxx',
          description:'xxx' },
        }]
      }
    ]
  },{
    path: '/online',
    component: BaseLayout,
    meta: { title: '在线工具' },
      children: [
      {
        path: '/online/countdown',
        component: () => import('../views/online/countdown.vue'),
        meta: { title: '在线工具在线倒计时' , keywords:'在线倒计时节日倒计时',
        description:'节日倒计时|自定义倒计时'},
 
     
      },{
        
        path: '/online/json',
        component: () => import('../views/online/json.vue'),
        meta: { title: '在线工具在线JSON解析', keywords:'什么是JSON?',
        description:'JSON是一种取代XML的数据结构，和xml相比，它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。' },
      },{
        
        path: '/online/imgtobase',
        component: () => import('../views/online/imgtobase.vue'),
        meta: { title: '图片转base64在线编码', keywords:'base64转图片|base64在线转文件|互转',
        description:'图片转base64在线编码|base64转图片|base64在线转文件|互转' },
      },{
        
        path: '/online/bonus',
        component: () => import('../views/online/bonus.vue'),
        meta: { title: '股票分红计算器', keywords:'在线计算器',
        description:'股票分红计算器在线' },
      }
      
    ]
  }
];

const routes = [...inRoutes]



const router = createRouter({
  // 使用 hash 模式构建路由（ url中带 # 号的那种)
  //history: createWebHashHistory(),
  // 使用 history 模式构建路由 （ url 中没有 # 号，但生产环境需要特殊配置）
  history:createWebHistory(),
  routes
})
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {


 /*********动态修改keywords和description*************/
 if(Object.keys(to.meta).length>0 && to.matched.length>0){
  let document=window.document as any;
  let this_meta=to.matched[to.matched.length-1].meta
  if(this_meta.title)document.title=this_meta.title
  let head = document.getElementsByTagName('head');
  let meta_keyword=document.createElement('meta');
  if(document.querySelector('meta[name="keywords"]')){
    document.querySelector('meta[name="keywords"]').setAttribute('content',this_meta.keywords)
  }else{
    meta_keyword.setAttribute('name','keywords')
    meta_keyword.setAttribute('content',this_meta.keywords)
    head[0].appendChild(meta_keyword)
  }
  let meta_description=document.createElement('meta');
  if(document.querySelector('meta[name="description"]')){
    document.querySelector('meta[name="description"]').setAttribute('content',this_meta.description)
  }else{
    meta_description.setAttribute('name','description')
    meta_description.setAttribute('content',this_meta.description)
    head[0].appendChild(meta_description)
  }
 }

  if(to.meta.requireAuth) {
    next();
  } else if(to.matched.length == 0) {
    next({path: '/error/404'})
  }  else {
    next();
  }
})

let win = window as any;

http.post("/v1/api/siteconfig/getsiteconfig", { domainname: win.location.host }).then((resp: any) => {
  const appStore = useAppStore();
  if (resp.success) {
    appStore.isgrey = resp.data.isgrey;
    win.document.getElementsByTagName('html')[0].className = appStore.isgrey ? "isgrey" : "";

  }
}).catch((resp: any) => {

})
export default router